﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Data.Linq;
using System.Linq;

namespace TestApplication
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        /*private void ProtectSection(string sectionName, string provider)
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            ConfigurationSection section = config.GetSection(sectionName);

            if (section != null && !section.SectionInformation.IsProtected)
            {
                section.SectionInformation.ProtectSection(provider);
                config.Save();
            }
            else
            {
                MessageBox.Show("");
            }
        }*/
        static public void ProtectSection()
        {

            string constr = ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString;
            Configuration conf = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            conf.ConnectionStrings.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");
            conf.Save(ConfigurationSaveMode.Full);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            ProtectSection();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string constr = ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString;
            Configuration conf = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            //conf.ConnectionStrings.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");
            conf.ConnectionStrings.SectionInformation.UnprotectSection();
            conf.Save(ConfigurationSaveMode.Full);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string constr = ConfigurationManager.ConnectionStrings["testconnstr"].ConnectionString;
            DataContext ctx = new DataContext(constr);
            /*using (AdventureWorksEntities context = new AdventureWorksEntities())
            {
                IQueryable<Product> productsQuery = from product in context.Products
                                                    select product;

                Console.WriteLine("Product Names:");
                foreach (var prod in productsQuery)
                {
                    Console.WriteLine(prod.Name);
                }
            }*/
            //IQueryable<Product> productsQuery = from product in ctx.Products
            //                                    select product;
            //Console.WriteLine("Product Names:");
            //foreach (var prod in productsQuery)
           // {
             //   Console.WriteLine(prod.Name);
            //}
            //Console.WriteLine("");
            Table<Product> product = ctx.GetTable<Product>();
            var prodq = from p in product
                        select p;
            foreach (var prod in prodq)
            {
                
                label1.Text += prod.ProductName;
            }
        }

    }
}
